from flask import Flask,render_template,request,redirect,session

app = Flask(__name__)
# session 设置secret_key
app.secret_key = 'flask_hello'
app.debug=True
# user
USER_DICT = {
    '1': {"name":'大卫','age': 18},
    '2': {"name":'alex','age': 20},
    '3': {"name":'ales','age': 23},
}

@app.route('/', methods=['GET', 'POST'])
@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'GET':
        return render_template('login.html')
    user = request.form.get('user')
    password = request.form.get('password')
    print(user)
    print(password)
    if user == 'alex' and password == '123':
        # 用户信息添加到session
        session['user_info'] = user
        return redirect('/index')
    else:
        return render_template('login.html', msg='用户名或密码错误')
        # return render_template('login.html', **{'msg':'用户名或密码错误'})

@app.route('/index')
def index():
    user_info = session.get('user_info')
    if not user_info:
        return redirect('/login')
    return render_template('index.html', user_dict = USER_DICT)

@app.route('/logout')
def logout():
    del session['user_info']
    user_info = session.get('user_info')
    if not user_info:
        return redirect('/login')
    return render_template('index.html')


@app.route('/detail')
def detail():
    res = request.args.get('uid')
    info = USER_DICT.get(res)
    return render_template('detail.html', info = info)


if __name__ == "__main__":
    app.run()